package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.IJoinSplitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 拆卸后的价格要高于原价格
 服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class JoinSplitServiceImpl extends ServiceImpl<JoinSplitMapper, JoinSplit> implements IJoinSplitService {
    @Autowired
    JoinSplitMapper joinSplitMapper;
    @Autowired
    JoinSplitNewGoodsMapper joinSplitNewGoodsMapper;
    @Autowired
    JoinSplitOldGoodsMapper joinSplitOldGoodsMapper;
    @Autowired
    GoodsStockMapper goodsStockMapper;
    @Autowired
    GoodsSnMapper goodsSnMapper;
    @Autowired
    GoodsSnLogMapper goodsSnLogMapper;

    @Override
    public boolean checkNo(String no, String companyId) {
        return joinSplitMapper.selectCount(new QueryWrapper<JoinSplit>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public boolean checkClose(JoinSplit joinSplit) {
        return joinSplitMapper.selectCount(new QueryWrapper<JoinSplit>().eq("id",joinSplit.getId()).eq("close",true))  > 0;
    }

    @Override
    public
    void insert(JoinSplit joinSplit,
                List<JoinSplitOldGoods> oldGoodsList,
                List<GoodsSn> oldGoodsSnList,
                List<GoodsSnLog> oldGoodsSnLogList,
                List<JoinSplitNewGoods> newGoodsList,
                List<GoodsSn> newGoodsSnList,
                List<GoodsSnLog> newGoodsSnLogList){

        joinSplitMapper.insert(joinSplit);
        joinSplitOldGoodsMapper.insertBatch(oldGoodsList);
        joinSplitNewGoodsMapper.insertBatch(newGoodsList);

        JoinSplitVo joinSplitVo = joinSplitMapper.getVoById(joinSplit.getId());
        processGoodsStock(1,joinSplitVo);

        if (!Utils.isEmpty(oldGoodsSnList)){
            goodsSnMapper.outByList(joinSplitVo.getOldHouseId(), oldGoodsSnList);
            goodsSnLogMapper.insertBatch(oldGoodsSnLogList);
        }

        if (!Utils.isEmpty(newGoodsSnList)){
            goodsSnMapper.insertBatch(newGoodsSnList);
            goodsSnLogMapper.insertBatch(newGoodsSnLogList);
        }
    }

    @Override
    public void close(JoinSplit joinSplit) {
        JoinSplitVo joinSplitVo = joinSplitMapper.getVoById(joinSplit.getId());
        processGoodsStock(2,joinSplitVo);

        //原单序列号需要入库
        if (!Utils.isEmpty(joinSplitVo.getJoinSplitOldGoodsVoList())) {
            List<GoodsSn> list = PdosUtils.genGoodsSnList(joinSplitVo.getJoinSplitOldGoodsVoList());
            if (!Utils.isEmpty(list)) {
                goodsSnMapper.inByList(joinSplitVo.getOldHouseId(), list);
            }
        }

        //新单商品序列号需要出库
        if (!Utils.isEmpty(joinSplitVo.getJoinSplitNewGoodsVoList())) {
            List<GoodsSn> list = PdosUtils.genGoodsSnList(joinSplitVo.getJoinSplitNewGoodsVoList());
            if (!Utils.isEmpty(list)) {
                goodsSnMapper.outByList(joinSplitVo.getNewHouseId(), list);
            }
        }

        //单据本身关闭
        JoinSplit temp = new JoinSplit();
        temp.setId(joinSplit.getId());
        temp.setClose(true);
        joinSplitMapper.updateById(temp);
    }

    @Override
    public List<JoinSplitVo> listVo(String companyId,
                                    Integer page,
                                    Integer limit,
                                    Boolean close,
                                    String search,
                                    String no,
                                    String goodsName,
                                    String remark,
                                    Date date1,
                                    Date date2,
                                    String oldStoreId,
                                    String newStoreId,
                                    String userStoreId,
                                    String userId,
                                    Date makeTime1,
                                    Date makeTime2,
                                    String oldHouseId,
                                    String newHouseId,
                                    String makeUserId) {
        return joinSplitMapper.listVo(companyId,
                Utils.genPage(page,limit),
                limit,
                close,
                search,
                no,
                goodsName,
                remark,
                date1,
                date2,
                oldStoreId,
                newStoreId,
                userStoreId,
                userId,
                makeTime1,
                makeTime2,
                oldHouseId,
                newHouseId,
                makeUserId);
    }

    @Override
    public long count(String companyId,
                      Boolean close,
                      String search,
                      String no,
                      String goodsName,
                      String remark,
                      Date date1,
                      Date date2,
                      String oldStoreId,
                      String newStoreId,
                      String userStoreId,
                      String userId,
                      Date makeTime1,
                      Date makeTime2,
                      String oldHouseId,
                      String newHouseId,
                      String makeUserId) {
        return joinSplitMapper.count(companyId,
                close,
                search,
                no,
                goodsName,
                remark,
                date1,
                date2,
                oldStoreId,
                newStoreId,
                userStoreId,
                userId,
                makeTime1,
                makeTime2,
                oldHouseId,
                newHouseId,
                makeUserId);
    }

    @Override
    public JoinSplitVo getVoById(String id) {
        return joinSplitMapper.getVoById(id);
    }

    @Override
    public List<JoinSplitVo> listVoByIds(List<String> ids) {
        return joinSplitMapper.listVoByIds(ids);
    }





    /**
     *
     * @param type 1-新增组装拆卸单
     * @param joinSplitVo
     */
    private void processGoodsStock(int type, JoinSplitVo joinSplitVo){
        List<GoodsStock> goodsStockList = new ArrayList<>();
        if (!Utils.isEmpty(joinSplitVo.getJoinSplitOldGoodsVoList())) {
            for (JoinSplitOldGoodsVo vo : joinSplitVo.getJoinSplitOldGoodsVoList()) {
                BigDecimal number = vo.getNumber().multiply(vo.getTimes());
                //原清单需要减去库存;作废单据就增加库存
                if (type == 1) {
                    number = number.negate();
                }

                GoodsStock goodsStock = new GoodsStock();
                goodsStock.setId(UuidUtils.getUUID());
                goodsStock.setGoodsId(vo.getGoodsId());
                goodsStock.setHouseId(joinSplitVo.getOldHouseId());
                goodsStock.setNum(number); //数量*换算倍数
                goodsStock.setTime(new Date());
                goodsStock.setCompanyId(joinSplitVo.getCompanyId());
                goodsStockList.add(goodsStock);
            }
        }

        if (!Utils.isEmpty(joinSplitVo.getJoinSplitNewGoodsVoList())) {
            for (JoinSplitNewGoodsVo vo : joinSplitVo.getJoinSplitNewGoodsVoList()) {
                BigDecimal number = vo.getNumber().multiply(vo.getTimes());

                //新清单需要增加库存;作废单据就减去库存
                if (type == 2) {
                    number = number.negate();
                }

                GoodsStock goodsStock = new GoodsStock();
                goodsStock.setId(UuidUtils.getUUID());
                goodsStock.setGoodsId(vo.getGoodsId());
                goodsStock.setHouseId(joinSplitVo.getNewHouseId());
                goodsStock.setNum(number); //数量*换算倍数
                goodsStock.setTime(new Date());
                goodsStock.setCompanyId(joinSplitVo.getCompanyId());
                goodsStockList.add(goodsStock);
            }
        }
        goodsStockMapper.insertBatch(goodsStockList);
    }
}
